# WS client example

import asyncio
import websockets
from time import sleep

DEVICE_ID = "8"


def downloadTask(taskTodo):
    print("下载文件中")
    sleep(3)
    print("打印文件中")
    sleep(2)
    print("打印{}完成!".format(taskTodo))


async def query_by_http():
    print("查询文件中")
    sleep(2)
    print("查询完成!")


async def query_by_socket():
    try:
        await query_by_http()
        async with websockets.connect('ws://localhost:8080/websocket/'+DEVICE_ID) as websocket:
            while True:
                # await websocket.send(number)
                taskTodo = await websocket.recv()
                print(f"< {taskTodo}")
                downloadTask(taskTodo)
        
    except websockets.exceptions.ConnectionClosed:
        print("Connection closed.")


def main():
    asyncio.get_event_loop().run_until_complete(query_by_socket())


if __name__ == "__main__":
    main()
